package ltd.itlover.yqblog.blog;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import ltd.itlover.yqblog.R;
import ltd.itlover.yqblog.entity.BlogListBrief;
import ltd.itlover.yqblog.utils.DateUtil;

//博客列表的数据加载器
public class BlogAdapter extends RecyclerView.Adapter<BlogAdapter.ViewHolder> {
    private List<BlogListBrief> blogListBriefs;

    static class ViewHolder extends RecyclerView.ViewHolder{

        //用户名
        TextView textUsername;

        //博客的发表时间
        TextView textBlogTime;

        //博客标题
        TextView textBLogTitle;

        //博客封面图
        ImageView coverImag;

        //博客分类名
        TextView textCategoryName;

        //博客浏览量
        TextView textBrowsedNum;

        //博客点赞数
        TextView textLikeNum;

        //博客收藏量
        TextView textCollectedNum;

        //博客的评论数
        TextView textCommentNum;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            textUsername = (TextView) itemView.findViewById(R.id.text_username);
            textBlogTime = (TextView) itemView.findViewById(R.id.text_blog_time);
            textBLogTitle = (TextView) itemView.findViewById(R.id.text_blog_title);
            coverImag = (ImageView) itemView.findViewById(R.id.cover_img);
            textCategoryName = (TextView) itemView.findViewById(R.id.text_category_name);
            textBrowsedNum = (TextView) itemView.findViewById(R.id.text_browsed_num);
            textLikeNum = (TextView) itemView.findViewById(R.id.text_liked_num);
            textCollectedNum = (TextView) itemView.findViewById(R.id.text_collected_num);
            textCommentNum = (TextView) itemView.findViewById(R.id.text_comment_num);
        }
    }

    //拿到要渲染的数据
    public BlogAdapter(List<BlogListBrief> blogListBriefs){
        this.blogListBriefs = blogListBriefs;
    }

    @NonNull
    @Override
    public BlogAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载列表单项的布局文件
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item, parent, false);
        ViewHolder viewHolder = new  ViewHolder(view);


        //设置列表的每一项的点击事件
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //获取到被点击的博客的数据
                int position = viewHolder.getAdapterPosition();
                BlogListBrief blogListBrief = blogListBriefs.get(position);

                //跳转到博客详情页面
                Intent intent = new Intent(MainActivity.getMainContext(), BlogDetailActivity.class);
                intent.putExtra("blogDetail", blogListBrief);
                MainActivity.getMainContext().startActivity(intent);
            }
        });

        return viewHolder;
    }

    /**
     * 渲染数据到每个item中
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull BlogAdapter.ViewHolder holder, int position) {
        BlogListBrief blogListBrief = blogListBriefs.get(position);

        holder.textUsername.setText(blogListBrief.getUsername());
        holder.textBLogTitle.setText(blogListBrief.getTitle());
        holder.textBlogTime.setText(DateUtil.dateToString(blogListBrief.getCreated()));
        holder.textLikeNum.setText(blogListBrief.getLikeNum().toString());
        holder.textCategoryName.setText(blogListBrief.getCategoryName().toString());
        holder.textBrowsedNum.setText(blogListBrief.getIsBrowsedNum().toString());
        holder.textCollectedNum.setText(blogListBrief.getIsCollectNum().toString());
        holder.textCommentNum.setText(blogListBrief.getCommentNum().toString());
    }

    @Override
    public int getItemCount() {
        return blogListBriefs.size();
    }
}
